home *** CD-ROM | disk | FTP | other *** search
/ Experimental BBS Explossion 3 / Experimental BBS Explossion III.iso / games / nhak_src.zip / RND.C < prev    next >
C/C++ Source or Header  |  1993-03-16  |  2KB  |  125 lines

  1. /*    SCCS Id: @(#)rnd.c    3.0    87/07/06
  2.  */
  3. /* NetHack may be freely redistributed.  See license for details. */
  4.  
  5. #include    "hack.h"
  6.  
  7. #if defined(LINT) && defined(UNIX)    /* rand() is long... */
  8. extern int rand();
  9. #define RND(x)    (rand() % x)
  10. #else /* LINT */
  11. /* rand() is either random() or lrand48() - see config.h. */
  12. #ifdef UNIX
  13. #define RND(x)    (int)(Rand() % (long)(x))
  14. #else
  15. /* Good luck: the bottom order bits are cyclic. */
  16. #define RND(x)    (int)((Rand()>>3) % (x))
  17. #endif
  18. #endif /* LINT */
  19.  
  20. #ifdef OVL2
  21.  
  22. int
  23. rn1(x,y)    /* y <= rn1(x,y) < (y+x) */
  24. register int x, y;
  25. {
  26.     return(RND(x)+y);
  27. }
  28.  
  29. #endif /* OVL2 */
  30. #ifdef OVL0
  31.  
  32. int
  33. rn2(x)        /* 0 <= rn2(x) < x */
  34. register int x;
  35. {
  36. #ifdef DEBUG
  37.     if (x == 0) {
  38.         impossible("rn2(0) attempted");
  39.         return(0);
  40.     }
  41. #endif
  42.     return(RND(x));
  43. }
  44.  
  45. #endif /* OVL0 */
  46. #ifdef OVLB
  47.  
  48. int
  49. rnl(x)        /* 0 <= rnl(x) < x; sometimes subtracting Luck */
  50. register int x;    /* good luck approaches 0, bad luck approaches (x-1) */
  51. {
  52.     register int i = RND(x);
  53.  
  54.     if (Luck && rn2(50 - Luck)) {
  55.         i -= (x <= 15 && Luck >= -5 ? Luck/3 : Luck);
  56.         if (i < 0) i = 0;
  57.         else if (i >= x) i = x-1;
  58.     }
  59.  
  60.     return i;
  61. }
  62.  
  63. #endif /* OVLB */
  64. #ifdef OVL0
  65.  
  66. int
  67. rnd(x)        /* 1 <= rnd(x) <= x */
  68. register int x;
  69. {
  70. #ifdef DEBUG
  71.     if (x == 0) {
  72.         impossible("rnd(0) attempted");
  73.         return(1);
  74.     }
  75. #endif
  76.     return(RND(x)+1);
  77. }
  78.  
  79. #endif /* OVL0 */
  80. #ifdef OVL1
  81.  
  82. int
  83. d(n,x)        /* n <= d(n,x) <= (n*x) */
  84. register int n, x;
  85. {
  86.     register int tmp = n;
  87.  
  88.     while(n--) tmp += RND(x);
  89.     return(tmp);
  90. }
  91.  
  92. #endif /* OVL1 */
  93. #ifdef OVLB
  94.  
  95. int
  96. rne(x)      /* by stewr 870807 */
  97. register int x;
  98. {
  99.     register int tmp = 1;
  100.     while(!rn2(x)) tmp++;
  101.     return(min(tmp,(u.ulevel < 15) ? 5 : (int)u.ulevel/3));
  102. }
  103.  
  104. #ifdef THEOLOGY
  105. int
  106. rnz(i)
  107. int i;
  108. {
  109. # ifdef LINT
  110.     int x = i;
  111.     int tmp = 1000;
  112. # else
  113.     register long x = i;
  114.     register long tmp = 1000;
  115. # endif
  116.     tmp += rn2(1000);
  117.     tmp *= rne(4);
  118.     if (rn2(2)) { x *= tmp; x /= 1000; }
  119.     else { x *= 1000; x /= tmp; }
  120.     return((int)x);
  121. }
  122. #endif
  123.  
  124. #endif /* OVLB */
  125.